package cn.myfirefly.moming.portal.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.myfirefly.moming.common.entity.MMUser;
import cn.myfirefly.moming.common.model.RoleDto;
import cn.myfirefly.moming.common.model.UserDto;
import cn.myfirefly.moming.common.model.UserFilterDto;
import cn.myfirefly.moming.common.service.UserService;
import cn.myfirefly.moming.portal.entity.MMRole;
import cn.myfirefly.moming.portal.service.RoleService;
import cn.myfirefly.core.BaseController;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.annotation.BjuiDataGridFilterDtoWrap;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.core.model.ResultDto;
import cn.myfirefly.utils.StrUtil;

@Controller
@RequestMapping("user")
public class UserController extends BaseController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private RoleService roleService;
	
	@RequestMapping("index.do")
	public String index(HttpServletRequest request, 
			HttpServletResponse response) {
		return "user/index";
	}
	
	@RequestMapping("edit.do")
	public String edit(HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "user/edit";
	}
	
	@RequestMapping("add.do")
	public String add(HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "user/edit";
	}
	
	@RequestMapping("save.do")
	@ResponseBody
	public ResultDto save(UserDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		warpModifyInfo(dto, request);
		String msg = userService.save(dto);
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("保存成功");
	}
	
	@RequestMapping("list.do")
	@ResponseBody
	@BjuiDataGridFilterDtoWrap
	public PageResultDto<MMUser> list(UserFilterDto dto, HttpServletRequest request) {
		PageResultDto<MMUser> result = null;
		if (getLoginUser(request).isAdmin()) {
			result = userService.getList(dto, true);
		} else {
			result = userService.getList(dto, false);
		}
		return result;
	}
	
	@RequestMapping("getUser.do")
	@ResponseBody
	public ResultDto getUser(HttpServletRequest request, 
			HttpServletResponse response) {
		String userSn = request.getParameter("userSn");
		UserDto user = userService.getUser(Integer.valueOf(userSn));
		return success(user);
	}
	
	@RequestMapping("del.do")
	@ResponseBody
	public ResultDto del(UserDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String msg = userService.del(dto.getUserSn());
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("删除成功");
	}
	
	@RequestMapping("password.do")
	public String passwd(HttpServletRequest request, 
			HttpServletResponse response) {
		request.setAttribute("userSn", request.getParameter("userSn"));
		return "user/setPasswd";
	}
	
	@RequestMapping("setPassword.do")
	@ResponseBody
	public ResultDto setPasswd(UserDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String passwd = request.getParameter("passwd");
		String pwdConfirm = request.getParameter("passwd_confirm");
		if(pwdConfirm.equals(passwd)){
			String msg = userService.setPasswd(dto, passwd);
			if (!Const.ResultCode.SUCCESS.equals(msg)) {
				return error(msg);
			}
		} else {
			return error("两次密码输入不一致!");
		}
		return success("密码重置成功！");
	}
	
	@RequestMapping("changeStatus.do")
	@ResponseBody
	public ResultDto changeStatus(UserDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String msg = userService.changeStatus(dto);
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success();
	}
	
	@RequestMapping("lookUpRole.do")
	public String lookupRole(RoleDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		PageResultDto<MMRole> resultDto = roleService.getList(dto);
		request.setAttribute("resultDto", resultDto);
		request.setAttribute("roleName", dto.getRoleName());
		
		String userSn = request.getParameter("userSn");
		request.setAttribute("userSn", userSn);
		if (StrUtil.notBlank(userSn)) {
			List<RoleDto> list = roleService.getRoles(Integer.valueOf(userSn));
			// 已选的角色
			request.setAttribute("roles", list);
		}
		return "user/role-lookup";
	}
}
